//
//  MYHintView.m
//  HolyShit
//
//  Created by Robin on 2017/2/15.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "MYHintView.h"

@interface MYHintView ()

@property (nonatomic, strong) UIImageView *hintImgV;
@property (nonatomic, strong) UILabel *hintLb;
@property (nonatomic, strong) UIView *superView;

@end

@implementation MYHintView

- (instancetype)initWithSuperView:(UIView *)superView
{
    if (self = [super init]) {
        self.superView = superView;
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    [self.superView addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.superView);
    }];
    [self addSubview:self.hintImgV];
    [self.hintImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self.mas_bottom).multipliedBy(0.65).offset(-NavBarHeight * 0.6);
    }];
    [self addSubview:self.hintLb];
    [self.hintLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(self.hintImgV.mas_bottom).offset(15);
    }];
    self.hintState = MYHintStateHidden;
    @weakify(self);
    [self bk_whenTapped:^{
        @strongify(self);
        if (self.reloadBlock && self.hintState != MYHintStateLoading) {
            self.hidden = YES;
            self.reloadBlock();
        }
    }];
}

- (void)updateViewState
{
    if (self.hintState == MYHintStateHidden) {
        self.hidden = YES;
        [self.superView sendSubviewToBack:self];
    }
    else{
        self.hidden = NO;
        self.backgroundColor = self.superView.backgroundColor;
        [self.superView bringSubviewToFront:self];
    }
    switch (self.hintState) {
        case MYHintStateNoData:
            self.hintImgV.image = [UIImage imageNamed:@"hintNoData"];
            self.hintLb.text = @"这里什么都没有\n去看看别的或者点击屏幕刷新一下";
            break;
        case MYHintStateNetError:
            self.hintImgV.image = [UIImage imageNamed:@"hintNetError"];
            self.hintLb.text = @"咦?网络不见了\n请检查网络连接后点击屏幕刷新";
            break;
        case MYHintStateLoading:{
            NSUInteger loadingType = 1;
            self.hintImgV.image = [YYImage imageNamed:[NSString stringWithFormat:@"loading%tu.gif", loadingType]];
            self.hintLb.text = @"正在加载中......";
        }
            break;
        default:
            break;
    }
}

#pragma mark - getter & setter

- (UIImageView *)hintImgV
{
    if (!_hintImgV) {
        _hintImgV = [YYAnimatedImageView new];
        _hintImgV.contentMode = UIViewContentModeCenter;
    }
    return _hintImgV;
}

- (UILabel *)hintLb
{
    if (!_hintLb) {
        _hintLb = [UILabel labelWithFont:MYFont(Font32) color:HEXCOLOR(HSColorGray)];
        _hintLb.numberOfLines = 0;
        _hintLb.textAlignment = NSTextAlignmentCenter;
    }
    return _hintLb;
}

- (void)setHintState:(MYHintState)hintState
{
    _hintState = hintState;
    [self updateViewState];
}

@end
